package com.framework.modules.base.service.impl;

import com.framework.common.sms.SmsResponse;
import com.framework.common.utils.DateUtils;
import com.framework.common.sms.CloudSmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.Query;

import com.framework.modules.base.dao.ValidateCodeDao;
import com.framework.modules.base.entity.ValidateCodeEntity;
import com.framework.modules.base.service.ValidateCodeService;


@Service("validateCodeService")
public class ValidateCodeServiceImpl extends ServiceImpl<ValidateCodeDao, ValidateCodeEntity> implements ValidateCodeService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<ValidateCodeEntity> page = this.selectPage(
                new Query<ValidateCodeEntity>(params).getPage(),
                new EntityWrapper<ValidateCodeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public ValidateCodeEntity getLastValidCodeEntry(String mobile, Integer exprirdTimeStamp) {
        Integer now = DateUtils.getSecondTimestamp(new Date());
        return baseMapper.getLastValidCodeEntry(mobile,now - exprirdTimeStamp);
    }

    @Override
    public Integer checkCodeCount(String mobile, Integer start, Integer end) {
        return baseMapper.checkCodeCount(mobile,start,end);
    }

    @Override
    public Integer checkDailyCodeCount(String mobile) {
        Date now = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        Integer start = DateUtils.getSecondTimestamp(cal.getTime());
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);

        Integer end = DateUtils.getSecondTimestamp(cal.getTime());

        return baseMapper.checkCodeCount(mobile,start,end);
    }

    @Autowired
    private CloudSmsService cloudSmsService;

    @Override
    public SmsResponse sendValidateCode(String mobs, String validateCode) {

        SmsResponse res = cloudSmsService.send_validate_code(mobs,validateCode);
        if(res.getState() == SmsResponse.State.Success){
            ValidateCodeEntity entity = new ValidateCodeEntity();
            entity.setMob(mobs);
            entity.setState(0);
            entity.setCode(validateCode);
            entity.setAdddate(DateUtils.getSecondTimestamp(new Date()));
            entity.setAddlog(res.getRaw());
            entity.setType("sys");
            entity.setUid(0);
            this.insert(entity);
        }
        return res;
    }

    @Override
    public Integer updateState(Integer id, Integer state) {
        return this.baseMapper.updateState(id,state);
    }

}
